//
//  SKUDefultQuestionHeadCell.m
//  ZITHER
//
//  Created by weixin on 2018/11/10.
//  Copyright © 2018 weixn. All rights reserved.
//

#import "SKUDefultQuestionHeadCell.h"

@implementation SKUDefultQuestionHeadCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    // Initialization code
    self.backgroundColor = [UIColor randomColor];
    [self setUI];
    [self setMasonry];
    self.viewBorder.backgroundColor = HexColor(0xf5f5f5);
    self.viewBorder.hidden = NO;
    [self.viewBorder mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self).offset(0);
        make.height.mas_equalTo(10);
        make.bottom.equalTo(self).offset(-10);
    }];
}

-(void)setMasonry{
//    [self.labName mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self).offset(14);
//        make.top.equalTo(self.mas_top).offset(35);
//        make.height.mas_equalTo(30);
//    }];
//
//    [self.anotherName mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self).offset(15);
//        make.top.equalTo(self.labName.mas_bottom).offset(-5);
//    }];
    [self.libelAge mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.top.equalTo(self.mas_top).offset(20);
        make.height.mas_equalTo(16);
    }];
    
    [self.libelAge1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.top.equalTo(self.libelAge.mas_bottom).offset(15);
        make.height.mas_equalTo(13);
    }];
    
    [self.RefreshV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-15);
        make.top.equalTo(self.libelAge.mas_top).offset(3);

//        make.centerY.equalTo(self.libelAge.mas_centerY).offset(0);
        make.height.width.mas_equalTo(32);
    }];
    
    [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.top.equalTo(self.libelAge1.mas_top).offset(23);
        make.height.mas_equalTo(16);
        make.bottom.equalTo(self).offset(-34);
    }];
    

}


-(void)setModel:(skuDetailModel *)model
{
    _model = model;
    
//    self.labName.text = model.goodName;
//    self.anotherName.text = model.goodNameNative;
    
    
//    self.libelAge.text = 【 model.skuTicketReward;
    self.RefreshV.model = model;
    self.RefreshV.radius =  [model.refreshTime convertCurrentTimeChangeHours];
//    self.libelAge.text = [NSString stringWithFormat:@"已询价%@天",model.skuTicket];
    NSString *str =  [self pleaseInsertStarTimeo:model.currentDateTime andInsertEndTime:[self getCurrentTimes]];
       
    self.libelAge.text = [NSString stringWithFormat:@"已询价%@天",str];

//    NSString *str = [NSString stringWithFormat:@"每次刷新询价可以获得%@张询价票",model.skuTicketReward];
//    self.libelAge1.text = str;

}

-(void)setUI{
//    [self addSubview:self.labName];
//    [self addSubview:self.anotherName];
    [self addSubview:self.libelAge];
    [self addSubview:self.libelAge1];
    [self addSubview:self.RefreshV];
    [self addSubview:self.closeBtn];
    
//    BaseImageV *image = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 1, 1) contentMode:2];
//    [image setImage:[UIImage imageNamed:@"sku_icon_tickect"]];
//    [self addSubview:image];
//    [image mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.libelAge.mas_right).offset(10);
//        make.bottom.equalTo(self.libelAge.mas_bottom).offset(-5);
//        make.width.height.mas_equalTo(20);
//    }];
    
}

-(BaseLab *)labName{
    if (!_labName) {
        _labName = [[BaseLab alloc]initializeFrame:CGRectMake(15, 450, 242, 60) text:@"30003/000P-8200" font:10 textColor:[UIColor blackColor] isBold:NO numberOfLines:1];
        
//        _labName.font = [UIFont fontWithName:@"CSD-Block-Bold" size:40];
        _labName.font = [UIFont fontWithName:@"Arial-BoldMT" size:30];

        _labName.adjustsFontSizeToFitWidth = YES;
        _labName.minimumScaleFactor = 10;
        
    }
    return _labName;
}

-(BaseLab *)anotherName{
    if (!_anotherName) {
        _anotherName = [[BaseLab alloc]initializeFrame:CGRectMake(15, 486, 28, 14) text:@"绿鬼" font:10 textColor:[UIColor blackColor] isBold:NO numberOfLines:1];
        _anotherName.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:14];
    }
    return _anotherName;
}

-(MainRefreshBezierPathV*)RefreshV{
    if (!_RefreshV) {
        _RefreshV = [[MainRefreshBezierPathV alloc]initWithFrame:CGRectMake(kScreenW-50,20+90, 32, 32)];
        
        UIImageView *image = nil;
        if (!kUSER_INFO_MANAGER.isSTViewControllerRefresh) {
            image = [self addImage];
        }
    
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        [[tap rac_gestureSignal] subscribeNext:^(id x) {
            NSLog(@"点击刷新");
            if (image != nil) {
                image.hidden = YES;
            }
            /* TODO: ================= 点击刷新 =================*/
            @weakify(self);
            [[serviceDefualt loadrefreshEnquiry:self.model.goodsId reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
                @strongify(self);
                if (x.success) {
                    [self.RefreshV setRadius:1];
                    [Tools showTips:@"刷新询价成功"];
                    
                }else{
                     [Tools showTips:x.errorInfo];
                }
            }];
        }];
        
        [self addGestureRecognizer:tap];
    
    }
    return _RefreshV;
}

-(BaseImageV *)addImage{
    BaseImageV *imageV = [[BaseImageV alloc]initializeFrame:CGRectMake(kScreenW/2.0-100, kScreenH-TabbarHeight-55, 200,55 ) contentMode:1];
    imageV.image = [UIImage imageNamed:@"功能引导页_刷新询价1"];
    imageV.backgroundColor = [UIColor clearColor];
    [self addSubview:imageV];
    [[imageV addTag]subscribeNext:^(id  _Nullable x) {
        imageV.hidden = YES;
        //        imageV = nil;
    }];
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_bottom).offset(-40);
        make.right.equalTo(self.mas_right).offset(-10);
//        make.centerY.equalTo(self.libelAge.mas_centerY).offset(0);
//        make.height.width.mas_equalTo(32);
    }];
    UserInfoManager *model = kUSER_INFO_MANAGER;
    model.isSTViewControllerRefresh = YES;
    [kUSER_INFO_MANAGER setUserInfoWithModel:model];
    return imageV;
}

-(BaseLab *)libelAge{
    if (!_libelAge) {
        _libelAge = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 1, 1) text:@"3.5" font:15 textColor:[UIColor blackColor] isBold:YES numberOfLines:1];
        _libelAge.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:15];
        _libelAge.textColor = [UIColor blackColor];
    }
    return _libelAge;
}

-(BaseLab *)libelAge1{
    if (!_libelAge1) {
        _libelAge1 = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 1, 1) text:@"1" font:12 textColor:HexColor(0x333333) isBold:NO numberOfLines:1];
        _libelAge1.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
        _libelAge1.textColor = [UIColor blackColor];
        _libelAge1.text = @"刷新活跃，有机会收到更多报价";
    }
    return _libelAge1;
}

-(BaseBtn *)closeBtn{
    if (!_closeBtn) {
        _closeBtn = [BaseBtn initCustomBtnImageAndTitles:@[@" 关闭询价"] images:@[@"sku_关闭询价"] color:@[baseColorTheme]];
        _closeBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15];
        @weakify(self);
        [[_closeBtn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            [[serviceDefualt loadendEnquiryPrice:self.model.userEnquiryId reponseObjctClass:nil] subscribeNext:^(ResponseData* x) {
                if (x.success) {
                    /* MARK: ================= 刷新首页数据 =================*/
                    //                [self setLoad];
                    [Tools showTips:@"终止询价成功"];
                    if (self.block) {
                        self.block();
                    }
                } else {
                    [Tools showTips:x.errorInfo];
                }
            }];
        }];
        
       
    }
    return _closeBtn;
}
- (NSString *)pleaseInsertStarTimeo:(NSString *)time1 andInsertEndTime:(NSString *)time2{
    // 1.将时间转换为date
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date1 = [formatter dateFromString:time1];
    NSDate *date2 = [formatter dateFromString:time2];
    // 2.创建日历
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit type = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    // 3.利用日历对象比较两个时间的差值
    NSDateComponents *cmps = [calendar components:type fromDate:date1 toDate:date2 options:0];
    // 4.输出结果
    
    NSLog(@"%@",[NSString stringWithFormat:@"%ld",cmps.day]);
    return [NSString stringWithFormat:@"%ld",cmps.day];
}
-(NSString*)getCurrentTimes{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    //现在时间,你可以输出来看下是什么格式
    
    NSDate *datenow = [NSDate date];
    
    //----------将nsdate按formatter格式转成nsstring
    
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    
    NSLog(@"currentTimeString =  %@",currentTimeString);
    
    return currentTimeString;
    
}

@end


